<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{title}}</title>
    <link rel="stylesheet" href="/apps/css/bootstrap.min.css">
    <link rel="stylesheet" href="/apps/css/fontawesome-free-6.4.0-web/css/all.min.css">
    <style>
        .poster-container {
            max-width: 100%;
            overflow: hidden;
        }
        .poster-preview {
            max-width: 100%;
            height: auto;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-top: 20px;
        }
        .search-results {
            max-height: 300px;
            overflow-y: auto;
        }
        .bg-image-item {
            cursor: pointer;
            transition: all 0.2s;
            border: 2px solid transparent;
        }
        .bg-image-item:hover {
            border-color: #0d6efd;
        }
        .bg-image-item.selected {
            border-color: #0d6efd;
            background-color: rgba(13, 110, 253, 0.1);
        }
        .bg-image-item img {
            width: 100%;
            height: 100px;
            object-fit: cover;
        }
        .loading-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 9999;
        }
        .loading-spinner {
            color: white;
            font-size: 24px;
        }
        .qrcode-switch {
            position: fixed;
            bottom: 20px;
            left: 20px;
            background-color: white;
            padding: 10px 15px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            z-index: 100;
        }
        .download-btn-fixed {
            position: fixed;
            bottom: 20px;
            right: 20px;
            z-index: 100;
        }
    </style>
</head>
<body>
    <div class="container py-4">
        <h1 class="mb-4">{{title}}</h1>

        <div class="row">
            <div class="col-md-6">
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="card-title mb-0">海报设置</h5>
                    </div>
                    <div class="card-body">
                        <form id="posterForm">
                            <!-- 背景图设置 -->
                            <div class="mb-3">
                                <label for="backgroundUrl" class="form-label">海报背景图网址</label>
                                <div class="input-group">
                                    <input type="text" class="form-control" id="backgroundUrl" name="backgroundUrl" value="{{backgroundUrl}}" placeholder="请输入背景图网址或使用搜索工具">
                                    <button type="button" class="btn btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#searchModal">
                                        <i class="fas fa-search"></i> 搜索
                                    </button>
                                </div>
                                <div class="form-text">输入完整的图片URL或使用搜索工具选择图片</div>
                            </div>

                            <!-- 小程序模块选择 -->
                            <div class="mb-3">
                                <label for="moduleView" class="form-label">跳转小程序模块</label>
                                <select class="form-select" id="moduleView" name="moduleView">
                                    <option value="">请选择模块</option>
                                    {{#each modules}}
                                    <option value="{{key}}" {{#if (eq ../moduleView key)}}selected{{/if}}>{{name}} - {{description}}</option>
                                    {{/each}}
                                </select>
                            </div>

                            <!-- 模块参数 -->
                            <div class="mb-3">
                                <label for="moduleParams" class="form-label">模块参数</label>
                                <input type="text" class="form-control" id="moduleParams" name="moduleParams" value="{{defaultParams}}" placeholder="例如: cid=123">
                                <div class="form-text">参数格式为: key1=value1&key2=value2</div>
                            </div>

                            <button type="button" id="generateBtn" class="btn btn-primary">
                                <i class="fas fa-magic"></i> 生成海报
                            </button>
                        </form>
                    </div>
                </div>

                <!-- 二维码信息 -->
                <div class="card mb-4" id="qrcodeInfoCard" style="display: none;">
                    <div class="card-header">
                        <h5 class="card-title mb-0">二维码信息</h5>
                    </div>
                    <div class="card-body">
                        <div class="mb-3">
                            <label class="form-label">二维码链接</label>
                            <input type="text" class="form-control" id="qrcodeUrl" readonly>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title mb-0">海报预览</h5>
                    </div>
                    <div class="card-body text-center poster-container">
                        <div id="posterPreview">
                            <p class="text-muted">生成海报后将在此处显示预览</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 切换码格式控件 -->
    <div class="qrcode-switch" id="qrcodeSwitchContainer" style="display: none;">
        <div class="form-check form-switch">
            <input class="form-check-input" type="checkbox" id="useWxacode" name="useWxacode" {{#if useWxacode}}checked{{/if}}>
            <label class="form-check-label" for="useWxacode">使用小程序码</label>
        </div>
        <div class="form-text">切换后需重新生成海报</div>
    </div>

    <!-- 下载按钮 -->
    <div class="download-btn-fixed" id="downloadBtnContainer" style="display: none;">
        <button type="button" id="downloadBtn" class="btn btn-success btn-lg">
            <i class="fas fa-download"></i> 保存海报
        </button>
    </div>

    <!-- 背景图搜索模态框 -->
    <div class="modal fade" id="searchModal" tabindex="-1" aria-labelledby="searchModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="searchModalLabel">搜索图片库</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="input-group mb-3">
                        <input type="text" class="form-control" id="searchKeyword" placeholder="输入图片关键字" value="北儿">
                        <button class="btn btn-primary" type="button" id="searchBtn">
                            <i class="fas fa-search"></i> 搜索
                        </button>
                    </div>

                    <div class="search-results">
                        <div id="searchResults" class="row g-3">
                            <p class="text-center text-muted">请输入关键字搜索图片</p>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="selectImageBtn" disabled>选择图片</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 加载中遮罩 -->
    <div class="loading-overlay" id="loadingOverlay" style="display: none;">
        <div class="loading-spinner">
            <i class="fas fa-spinner fa-spin"></i> 正在处理...
        </div>
    </div>

    <script src="/apps/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 初始化变量
            let selectedImageUrl = document.getElementById('backgroundUrl').value;
            let selectedImageId = null;

            // 搜索背景图
            document.getElementById('searchBtn').addEventListener('click', function() {
                const keyword = document.getElementById('searchKeyword').value.trim();
                if (!keyword) {
                    alert('请输入搜索关键字');
                    return;
                }

                // 显示加载中
                document.getElementById('searchResults').innerHTML = '<p class="text-center"><i class="fas fa-spinner fa-spin"></i> 正在搜索...</p>';

                // 发送搜索请求
                fetch(`/apps/tools/poster/search-background?keyword=${encodeURIComponent(keyword)}`)
                    .then(response => response.json())
                    .then(data => {
                        if (data.success) {
                            if (data.images.length === 0) {
                                document.getElementById('searchResults').innerHTML = '<p class="text-center text-muted">未找到匹配的图片</p>';
                                return;
                            }

                            // 显示搜索结果
                            let html = '';
                            data.images.forEach(image => {
                                html += `
                                    <div class="col-md-4">
                                        <div class="card bg-image-item" data-id="${image.id}" data-url="${image.url}">
                                            <img src="${image.url}" alt="${image.title}" class="card-img-top">
                                            <div class="card-body p-2">
                                                <p class="card-text small text-truncate">${image.title}</p>
                                            </div>
                                        </div>
                                    </div>
                                `;
                            });

                            document.getElementById('searchResults').innerHTML = html;

                            // 添加图片选择事件
                            document.querySelectorAll('.bg-image-item').forEach(item => {
                                item.addEventListener('click', function() {
                                    // 移除之前的选中状态
                                    document.querySelectorAll('.bg-image-item').forEach(el => {
                                        el.classList.remove('selected');
                                    });

                                    // 添加选中状态
                                    this.classList.add('selected');

                                    // 保存选中的图片信息
                                    selectedImageId = this.dataset.id;
                                    selectedImageUrl = this.dataset.url;

                                    // 启用选择按钮
                                    document.getElementById('selectImageBtn').disabled = false;
                                });
                            });
                        } else {
                            document.getElementById('searchResults').innerHTML = `<p class="text-center text-danger">${data.message}</p>`;
                        }
                    })
                    .catch(error => {
                        console.error('搜索错误:', error);
                        document.getElementById('searchResults').innerHTML = '<p class="text-center text-danger">搜索出错，请重试</p>';
                    });
            });

            // 选择图片按钮点击事件
            document.getElementById('selectImageBtn').addEventListener('click', function() {
                if (selectedImageUrl) {
                    document.getElementById('backgroundUrl').value = selectedImageUrl;

                    // 关闭模态框
                    const modal = bootstrap.Modal.getInstance(document.getElementById('searchModal'));
                    modal.hide();
                }
            });

            // 生成海报按钮点击事件
            document.getElementById('generateBtn').addEventListener('click', function() {
                const backgroundUrl = document.getElementById('backgroundUrl').value.trim();
                const moduleView = document.getElementById('moduleView').value;
                const moduleParams = document.getElementById('moduleParams').value.trim();
                const useWxacode = document.getElementById('useWxacode').checked;

                if (!backgroundUrl) {
                    alert('请输入或选择背景图网址');
                    return;
                }

                if (!moduleView) {
                    alert('请选择跳转小程序模块');
                    return;
                }

                // 显示加载中遮罩
                document.getElementById('loadingOverlay').style.display = 'flex';

                // 发送生成请求
                fetch('/apps/tools/poster/generate', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        backgroundUrl,
                        moduleView,
                        moduleParams,
                        useWxacode: useWxacode.toString()
                    })
                })
                .then(response => response.json())
                .then(data => {
                    // 隐藏加载中遮罩
                    document.getElementById('loadingOverlay').style.display = 'none';

                    if (data.success) {
                        // 显示海报预览
                        document.getElementById('posterPreview').innerHTML = `
                            <img src="${data.posterImage}" class="poster-preview" alt="海报预览">
                        `;

                        // 显示二维码信息
                        document.getElementById('qrcodeInfoCard').style.display = 'block';
                        document.getElementById('qrcodeUrl').value = data.qrcodeUrl;

                        // 显示下载按钮和切换控件
                        document.getElementById('downloadBtnContainer').style.display = 'block';
                        document.getElementById('qrcodeSwitchContainer').style.display = 'block';

                        // 设置下载按钮事件
                        document.getElementById('downloadBtn').onclick = function() {
                            const link = document.createElement('a');
                            link.href = data.posterImage;
                            link.download = `poster-${moduleView}-${Date.now()}.png`;
                            document.body.appendChild(link);
                            link.click();
                            document.body.removeChild(link);
                        };
                    } else {
                        alert(data.message || '生成海报失败');
                    }
                })
                .catch(error => {
                    console.error('生成海报错误:', error);
                    document.getElementById('loadingOverlay').style.display = 'none';
                    alert('生成海报出错，请重试');
                });
            });

            // 切换码格式后自动重新生成
            document.getElementById('useWxacode').addEventListener('change', function() {
                if (document.getElementById('posterPreview').querySelector('img')) {
                    document.getElementById('generateBtn').click();
                }
            });

            // 回车键搜索
            document.getElementById('searchKeyword').addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    e.preventDefault();
                    document.getElementById('searchBtn').click();
                }
            });
        });
    </script>
</body>
</html> 
